<?php

class Style extends Controller {

	function Style()
	{
		parent::Controller();
		
		$this->load->model('collection');
		$this->load->model('frame');
		
		$this->load->helper('file');
		$this->load->helper('form');
	}
	
	function index()
	{
		redirect('/rendering/', 'refresh');
	}

	function style_upload()
	{
		$config['upload_path'] = $this->config->item('dirUpload');
		$config['allowed_types'] = 'sty';

		$this->load->library('upload', $config);
	
		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
			
			print_r($error);
		}	
		else
		{
			$data = $this->upload->data();
			
			$file = $data['file_name'];
			$filename = substr($file, 0, (strlen($file)-4)); //enleve l'extension du nom du fichier
			$url = $_SERVER['HTTP_REFERER'];
					
			exec("sudo ./addstyle.sh ".$file);
						
			redirect('/rendering/getFramesFromCol'. substr(strrchr($url, "/"), 0, strlen(strrchr($url, "/"))), 'refresh');
		}
	}

	function change_style() 
	{

		$url = $_SERVER['HTTP_REFERER'];
		$idCol = substr(strrchr($url, "/"), 1, strlen(strrchr($url, "/"))); //recupere l'id de la collection dans l'url
		
		$this->load->model('theme');
		$tex_dir = $this->theme->getColDir($idCol); //recupere le rep de la collection dans la bdd
			
		exec('./changestyle.sh '.$tex_dir.' '.$_POST['liste_themes']);

		$this->theme->setColTheme($idCol, $_POST['liste_themes']);

		// regenere le pdf
		exec("./make.sh ". "uploads/".$tex_dir."/ ".$tex_dir);
		exec("./clean.sh ". "uploads/".$tex_dir."/ ".$tex_dir);

		// récupération de toutes les frames de la collections
		$frames = $this->frame->getFrames($idCol);
		$i = 0;
		
		// parcours des slides de la collection
		foreach($frames as $frame)
		{
			$idSlide = $frame['ID'];
		
			// création de la miniature de chaque frame
			exec("./makeThumbnail.sh ". "uploads/".$tex_dir . "/ ". $tex_dir .".pdf ".$i." ".$idSlide);
			$i++;

		}

		redirect('/rendering/getFramesFromCol/'. $idCol, 'refresh');
	}

}
?>
